﻿using System;
using System.Collections.Generic;
using System.Text;
using Mang24k99.Model.COMM;
using NHibernate;
using NHibernate.Expression;
using InfoGrant.Business.GRANT;
using InfoGrant.Model.GRANT;

namespace Mang24k99.Business.COMM
{
    public partial class BKtCommOrglist
    {

        public static void Create(MKtCommOrglist orglist)
        {
            MKtCommOrglist.Create(orglist);
			if (orglist.IsGrant==1)//如果是授权机构，则往授权机构表插入一条数据
			{
				MLtGrantOrgnize mo = CopyValue(orglist);
				CreateRootNode(orglist);
				GrantCreate(mo);
			}
        }
		/// <summary>
		/// 拷贝数据
		/// </summary>
		/// <param name="orglist"></param>
		/// <returns></returns>
		private static MLtGrantOrgnize CopyValue(MKtCommOrglist orglist)
		{
			MLtGrantOrgnize mo = new MLtGrantOrgnize();
			mo.ChSpell = orglist.ChineseSpell;
			mo.InLayer = orglist.OrangeLayer;
			mo.IsValidate = 1;
			mo.IsVirtualo = 1;
			mo.OrganName = orglist.OrgnizeName;
			mo.OrganNO = orglist.OrgnizeNO;
			mo.ParentOrgno = orglist.ParentNO;
			return mo;
		}
		/// <summary>
		/// 创建与授权机构表对应的机构根节点
		/// </summary>
		/// <param name="orglist">由于程序已经写死，维护的机构都是二级节点</param>
		public static void CreateRootNode(MKtCommOrglist orglist)
		{
			MKtCommOrglist parent = BKtCommOrglist.Find(orglist.ParentNO);
			MKtCommOrglist root = BKtCommOrglist.Find(parent.ParentNO);
			MLtGrantOrgnize grantParent = CopyValue(parent);
			MLtGrantOrgnize grantRoot = CopyValue(root);

			GrantCreate(grantRoot);
			GrantCreate(grantParent);
		}
		/// <summary>
		/// 创建一条授权机构表记录
		/// </summary>
		/// <param name="grant"></param>
		public static void GrantCreate(MLtGrantOrgnize grant)
		{
			MLtGrantOrgnize mo = BLtGrantOrgnize.Find(grant.OrganNO);
			if (mo==null)
			{
				grant.ChildmNum = 0;
				BLtGrantOrgnize.Create(grant);
				if (grant.ParentOrgno != null)
				{
					MLtGrantOrgnize parent = BLtGrantOrgnize.Find(grant.ParentOrgno);
					if (parent != null)
					{
						parent.ChildmNum += 1;
						BLtGrantOrgnize.Update(parent);
					}
					
				}
			}
			else //如果记录已经存在，则修改
			{
				BLtGrantOrgnize.Update(mo);
			}
		}
		/// <summary>
		/// 修改一条授权机构表记录
		/// </summary>
		/// <param name="grant"></param>
		public static void GrantUpdate(MLtGrantOrgnize grant)
		{
			MLtGrantOrgnize mo = BLtGrantOrgnize.Find(grant.OrganNO);
			if (mo == null)
			{
				grant.ChildmNum = 0;
				BLtGrantOrgnize.Create(grant);
				if (grant.ParentOrgno!=null)
				{
					MLtGrantOrgnize parent = BLtGrantOrgnize.Find(grant.ParentOrgno);
					if (parent != null)
					{
						parent.ChildmNum += 1;
						BLtGrantOrgnize.Update(parent);
					}
					
				}
				
			}
			else //如果记录存在，则修改
			{
				BLtGrantOrgnize.Update(mo);
			}
		}
		/// <summary>
		/// 删除一条授权机构表记录
		/// </summary>
		/// <param name="grant"></param>
		public static void GrantDelete(MLtGrantOrgnize grant)
		{
			MLtGrantOrgnize mo = BLtGrantOrgnize.Find(grant.OrganNO);
			if (mo != null)//如果记录存在，则删除
			{
				BLtGrantOrgnize.Delete(mo);
				if (grant.ParentOrgno!=null)
				{
					MLtGrantOrgnize parent = BLtGrantOrgnize.Find(grant.ParentOrgno);
					if (parent != null)
					{
						parent.ChildmNum -= 1;
						BLtGrantOrgnize.Update(parent);
					}
				}
			}
		}

        public static IList<MKtCommOrglist> FindAll()
        {
            return MKtCommOrglist.SelectAll();
        }

        public static MKtCommOrglist Find(object ID)
        {
            return MKtCommOrglist.SelectByPrimaryKey(ID);
        }

        public static IList<MKtCommOrglist> FindALLByProperty(string property, object value)
        {
            return MKtCommOrglist.SelectALLByProperty(property, value);
        }

        public static IList<MKtCommOrglist> FindALLSliced(int firstResult, int maxResult)
        {
            DetachedCriteria criteria = null;
            return MKtCommOrglist.SelectALLSliced(firstResult, maxResult, criteria);
        }
        /// <summary>
        /// 获得最大的机构编号
        /// </summary>
        /// <returns></returns>
        public static int GetMaxNum()
        {
            return MKtCommOrglist.GetMaxNum();
        }

        public static int SelectMaxOrder(string where)
        {
            return (int)MKtCommOrglist.SelectMaxOrder(where);
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="orglist"></param>
        public static void Update(MKtCommOrglist orglist)
        {
            MKtCommOrglist.Update(orglist);
			if (orglist.IsGrant == 1)
			{
				MLtGrantOrgnize mo = CopyValue(orglist);
				CreateRootNode(orglist);
				GrantCreate(mo);
			}
			else
			{
				MLtGrantOrgnize mo = CopyValue(orglist);
				GrantDelete(mo);
			}
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="orglist"></param>
        /// <returns></returns>
        public static bool Delete(MKtCommOrglist orglist )
        {
            try
            {
                MKtCommOrglist.Delete(orglist);
				MLtGrantOrgnize mo = CopyValue(orglist);
				GrantDelete(mo);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
           
        }
        public static IList<MKtCommOrglist> SelectByWhere(String where)
        {
            return (IList<MKtCommOrglist>)MKtCommOrglist.SelectByWhere(where);
        }
        
    }
}
